//
//  CoreViewController.m
//  BlocksKitLearn
//
//  Created by Elvis on 2017/8/17.
//  Copyright © 2017年 Elvis. All rights reserved.
//

#import "CoreViewController.h"
#import "CoreCellItem.h"

static NSString *cellIdentifier = @"UITableViewCell";

@interface CoreViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *table;

@property (copy, nonatomic) NSArray *titles;

@property (copy, nonatomic) NSArray *classes;

@property (strong, nonatomic) NSMutableArray *itemArray;

@end

@implementation CoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
    [self setUpData];
}


#pragma mark - Private

- (void)setUpUI{
    self.navigationItem.title = @"Core";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.view addSubview:self.table];
}

- (void)setUpData{
    self.itemArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.classes.count; ++i) {
        CoreCellItem *item = [CoreCellItem new];
        item.objClass = NSClassFromString(self.classes[i]);
        [self.itemArray addObject:item];
    }
}


#pragma mark - <UITableViewDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = self.titles[indexPath.row];
    return cell;
}


#pragma mark - <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CoreCellItem *item = self.itemArray[indexPath.row];
    BaseViewController *base = [[item.objClass alloc] init];
    [base setNavagationBarTitle:self.titles[indexPath.row]];
    [self.navigationController pushViewController:base animated:YES];
}

#pragma mark - GET

- (UITableView *)table{
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.rowHeight = 50;
    }
    return _table;
}

- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@"NSArray+Blocks",
                    @"NSDictionary+Blocks",
                    @"NSIndexSet+Blocks",
                    @"NSInvocation+Blocks",
                    @"NSSet+Blocks",
                    @"NSTimer+Blocks",
                    @"NSOrderSet+Blocks",
                    @"NSObject+BKAssociatedObjects",
                    @"NSObject+BKBlockExecution",
                    @"NSObject+BKBlockObservation"];
    }
    return _titles;
}

- (NSArray *)classes{
    if (!_classes) {
        _classes = @[@"NSArrayBlocksViewController",
                     @"NSDicitonaryBlocksViewController",
                     @"NSIndexSetViewController",
                     @"NSInvocationViewController",
                     @"NSSetBlocksViewController",
                     @"NSTimerBlcoksViewController",
                     @"NSOrderSetViewController",
                     @"BKAssociatedViewController",
                     @"BKBoclExecutionViewController",
                     @"BKObservationViewController"];
    }
    return _classes;
}

@end
